package wcg;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;


public class winchkgProp extends Properties {
	private String PropertiesFile = null;
	
	winchkgProp(String propfile){
		PropertiesFile=propfile;
	}
	//-------------------------------------------------------------
	// ファイルから読み出す
	public boolean Load(){
		boolean ret = false;
		InputStream stream;
		try {
			stream = new FileInputStream(PropertiesFile);
			try {
				loadFromXML(stream);
				stream.close();
				ret = true;
			} catch (InvalidPropertiesFormatException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			//e.printStackTrace();
		}
		return ret;
	}
	//-------------------------------------------------------------
	// ファイルへ書き出す
	public boolean  Save(){
		boolean ret=false;
		OutputStream stream;
		try {
			stream = new FileOutputStream(PropertiesFile);
			try {
				storeToXML(stream, "Winchkg Properties", "Shift_JIS");
				stream.flush();
				stream.close();
				ret=true;
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return ret;
	}
	/**
	 * int を取得
	 * @param sec
	 * @param def
	 * @return
	 */
	public int getInt(String sec,int def){
		int ret = def;
		String mes = getProperty(sec);
		if(mes==null) return ret;
		try{
			ret = Integer.parseInt(mes);
		} 
		catch(NumberFormatException e){
			ret = def;
		}
		return ret;
	}
	/**
	 * long を取得
	 * @param sec
	 * @param def
	 * @return
	 */
	public long getLong(String sec,long def){
		long ret = def;
		String mes = getProperty(sec);
		if(mes==null) return ret;
		try{
			ret = Long.parseLong(mes);
		} 
		catch(NumberFormatException e){
			ret = def;
		}
		return ret;
	}
	/**
	 * int のdata をセットする
	 * @param sec
	 * @param dat
	 */
	public void setInt(String sec,int dat){
		String mes = String.format("%d", dat);
		setProperty(sec,mes);
	}
	/**
	 * long dataをセットする
	 * @param sec
	 * @param dat
	 */
	public void setLong(String sec,long dat){
		String mes = String.format("%d", dat);
		setProperty(sec,mes);
	}
	//-----------------------------------------------------------------
	// boolean をゲット/セット
	public boolean getBoolean(String sec,boolean def){
		boolean ret = def;
		String mes = getProperty(sec);
		if(mes==null) return ret;
		if(0==mes.compareToIgnoreCase("true")){
			ret = true;
		}
		else {
			ret = false;
		}
		return ret;
	}
	/**
	 * booleanをセット
	 * @param sec
	 * @param dat
	 */
	public void setBoolean(String sec,boolean dat){
		if(dat) setProperty(sec,"true");
		else    setProperty(sec,"false");
	}
	/**
	 * String をgetする
	 * @param sec
	 * @param def
	 * @return
	 */
	public String getString(String sec,String def){
		String mes = getProperty(sec);
		if(mes==null) return def;
		return mes;
	}
	/**
	 * Stringをsetする
	 * @param sec
	 * @param dat
	 */
	public void setString(String sec,String dat){
		setProperty(sec,dat);
	}
	
	
}
